import ACTIONS from './action';

//计算
const evalute = state => {
    //解析用原名
    let {lastOperand,currentOperand,operation} = state;
    lastOperand=parseFloat(lastOperand);
    currentOperand=parseFloat(currentOperand);

    let res="";
    switch (operation) {
        case '+':
            res = lastOperand + currentOperand;
            break;
        case '-':
            res = lastOperand - currentOperand;
            break;
        case '*':
            res = lastOperand * currentOperand;
            break;    
        case '/':
            res = lastOperand / currentOperand;
            break;
        default:
            break;
    }
    return res.toString();
}

// 操作不独立 一个reducer即可
// 修改数据 每个type都要有对应行为
const reducer = (state={
    // 关联的数据
    currentOperand: "",
    lastOperand: "",
    operation: "",
    // 覆盖输入
    overwrite: false,
},action) => {
    switch (action.type) {
        case ACTIONS.ADD_DIGIT:
            // 计算出答案后输入就先全清再继续输入
            if(state.overwrite)
                return{
                    ...state,
                    currentOperand: "",
                    overwrite: false,
                }
            //取消前导0
            if(state.currentOperand === '0' && action.digit !== '.')
                return{
                    ...state,
                    currentOperand:action.digit,
                }
            //取消>1个的小数点
            if(action.digit === '.' && state.currentOperand.includes('.'))
                return state;
            //空打小数点补0
            if(action.digit === '.' && state.currentOperand === '')
                return{
                    ...state,
                    currentOperand: '0.',
                }    
            return{
                ...state,
                currentOperand: state.currentOperand + action.digit,
            }

        
        case ACTIONS.DELETE_DIGIT:
            // 答案后再删除就全清
            if(state.overwrite)
                return{
                    ...state,
                    currentOperand:"",
                    overwrite: false,
                }
            if(state.currentOperand === "")
                return state;
            return{
                ...state,
                // 删除最后一位
                currentOperand: state.currentOperand.slice(0,-1),
            }


        case ACTIONS.CHOOSE_OPERATION:
            //空按无反应
            if(state.lastOperand === "" && state.currentOperand === "")
                return state;
            //将下面的数放上去
            if(state.lastOperand === "")
                return{
                    ...state,
                    lastOperand: state.currentOperand,
                    operation: action.operation,
                    currentOperand: "",
                }
            //切换运算符
            if(state.currentOperand === "")
                return{
                ...state,
                operation: action.operation,
                }
            //上下都有数时先计算再把答案移到上面
            return{
                ...state,
                lastOperand: evalute(state),
                operation: action.operation,
                currentOperand: "",
            }
        

        case ACTIONS.CLEAR:
            return{
                ...state,
                currentOperand:"",
                lastOperand:"",
                operation:"",
            }
        

        case ACTIONS.EVALUATE:
            //运算式不完整
            if(state.currentOperand === ""||
                state.lastOperand === ""||
                state.operation ==="")
                return state;
            return{
                ...state,
                currentOperand: evalute(state),
                lastOperand: "",
                operation: "",
                overwrite:true,
            }


        default:
            return state;
    }
}

export default reducer;